//
//  UIView+CJPopupFrameAnimation.m
//  CJUIKitDemo
//
//  Created by ciyouzen on 15/11/12.
//  Copyright (c) 2015年 dvlproad. All rights reserved.
//

#import "UIView+CJPopupFrameAnimation.h"
#import <objc/runtime.h>
#import "UIView+CJPopupSuperviewSubview.h"
#import "UIView+CJPopupFrameAnimationNormal.h"
#import "UIView+CJPopupFrameAnimation3D.h"

#define CJPopupMainThreadAssert() NSAssert([NSThread isMainThread], @"UIView+CJPopupInView needs to be accessed on the main thread.");

static CGFloat kCJPopupAnimationDuration = 0.3;


static NSString *cjPopupAnimationTypeKey = @"cjPopupAnimationType";

@interface UIView () {
    
}
@property (nonatomic, copy) NSString *cjPopupViewHideFrameString;   /**< 弹出视图隐藏时候的frame */

@end



@implementation UIView (CJPopupFrameAnimation)

#pragma mark - runtime
//cjPopupAnimationType
- (CJAnimationType)cjPopupAnimationType {
    return [objc_getAssociatedObject(self, &cjPopupAnimationTypeKey) integerValue];
}

- (void)setCjPopupAnimationType:(CJAnimationType)cjPopupAnimationType {
    objc_setAssociatedObject(self, &cjPopupAnimationTypeKey, @(cjPopupAnimationType), OBJC_ASSOCIATION_ASSIGN);
}


#pragma mark - Event
- (void)cj_popupWithViewInfo:(CJBasePopupInfo *)popupInfo
                  isToBottom:(BOOL)isToBottom
{
    self.cjPopupInfo = popupInfo;
    
    UIView *realPopupView = popupInfo.popupView;
    CGRect popupViewShowFrame = popupInfo.popupViewShowFrame;
    
    [realPopupView cj_animateToShowFrame:popupViewShowFrame isToBottom:isToBottom animationType:CJAnimationTypeNormal showComplete:^{
        
    }];
}


/** 完整的描述请参见文件头部 */
- (void)cj_hidePopupView {
    CJBasePopupInfo *popupInfo = self.cjPopupInfo;
    NSAssert(popupInfo != nil, @"popupInfo为nil，请检查");
    
    CJAnimationType animationType = self.cjPopupAnimationType;
    [self cj_hideWithViewInfo:popupInfo animationType:animationType];
}



/*
 *  弹出视图
 *
 *  @param popupViewShowFrame   popupViewShowFrame
 *  @param isToBottom           isToBottom
 *  @param animationType        animationType
 *  @param showComplete         showComplete
 */
- (void)cj_animateToShowFrame:(CGRect)popupViewShowFrame
                   isToBottom:(BOOL)isToBottom
                animationType:(CJAnimationType)animationType
                 showComplete:(void(^ _Nullable)(void))showComplete
{
    self.cjPopupAnimationType = animationType;
    
    if (animationType == CJAnimationTypeNone) {
        self.frame = popupViewShowFrame;
        !showComplete ?: showComplete();
        
    } else if (animationType == CJAnimationTypeNormal) {
        [self cj_animateNormalToShowFrame:popupViewShowFrame isToBottom:isToBottom showComplete:showComplete];
        
    } else if (animationType == CJAnimationTypeCATransform3D) {
        [self cj_animate3DToShowFrame:popupViewShowFrame isToBottom:isToBottom showComplete:showComplete];
    }
}




#pragma mark - Private Method
- (void)cj_hideWithViewInfo:(CJBasePopupInfo *)popupInfo
              animationType:(CJAnimationType)animationType
{
    CJPopupMainThreadAssert();
    
    [self endEditing:YES];
    
    UIView *popupView = popupInfo.popupView;
    UIView *blankView = popupInfo.blankView;
    
    switch (animationType) {
        case CJAnimationTypeNone:
        {
            [popupView removeFromSuperview];
            break;
        }
        case CJAnimationTypeNormal:
        {
            [popupView cj_animateNormalToHideWithComplete:^(UIView * _Nonnull bView) {
                [bView removeFromSuperview];
            }];
            break;
        }
        case CJAnimationTypeCATransform3D:
        {
            [popupView cj_animate3DToHideWithComplete:^(UIView * _Nonnull bView) {
                [bView removeFromSuperview];
            }];
            break;
        }
    }
    
    [self removePopupContainer:blankView];
}



@end
